package juc;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author lzy
 * Create DateTime: 2018/7/17 9:53
 * @version v1.0
 * <p>
 * CopyOnWriteArrayList/CopyOnWriteArraySet:"写入并复制"
 * 注意：添加操作多时，效率低，因为每次添加时都会进行复制，开销非常大，并发迭代操作多时，选择该容器可以提高效率
 */
public class TestCopyOnWriteArrayList {
    public static void main(String[] args) {
        HelloThread ht = new HelloThread();

        for (int i = 0; i <10; i++) {
            new Thread(ht).start();
        }
    }
}

class HelloThread implements Runnable {
//    private static List<String> list = Collections.synchronizedList(new ArrayList<>());

    private static CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();

    static {
        list.add("AA");
        list.add("BB");
        list.add("CC");
    }

    @Override
    public void run() {
        Iterator<String> iterator = list.iterator();

        while (iterator.hasNext()) {
            System.out.println(iterator.next());
            list.add("aa");
        }
    }
}


